package 冒泡排序优化版;

import java.util.Arrays;

import static java.util.Collections.swap;

public class Test {
    static int count = 0;
    public static void main(String[] args) {
        int[] arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
        int n = arr.length-1;
        for (int j = 0; j < arr.length - 1; j++) {
            int last =0;
            for (int i = 0; i < n; i++) {
                count++;
                if (arr[i] > arr[i+1]){
                    int temp = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = temp;
                    last = i;

                }
            }
            n = last;
            if (n == 0){
                break;
            }


            }
        System.out.println("一共比较的次数："+ count);
        System.out.println(Arrays.toString(arr));
        }
    }

